#pragma once
#include "uart/ProtocolSender.h"
#include "storage/StoragePreferences.h"
#include "logic/global_variables.h"
#include <iomanip>  // std::fixed, std::setprecision
#include <sstream>  // std::ostringstream

/**
 * 注册定时器
 * 填充数组用于注册定时器
 * 注意：id不能重复
 */
static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
	//{0,  6000}, //定时器id=0, 时间间隔6秒
	//{1,  1000},
};

/**
 * 当界面构造时触发
 */
static void onUI_init(){
	mlightTimePtr->setTouchPass(true);
	mcustomWndPtr->hideWnd();
	switch(Global::Water::ledType){
		case 1:{
			mOnSwitchPtr->setSelected(true);
		}break;
		case 0:{
			mOffSwitchPtr->setSelected(true);

		}break;
		case 2:{
			mcustomSwitchPtr->setSelected(true);
			mcustomWndPtr->showWnd();
			mcustomBarPtr->setProgress(Global::Water::ledTime / 30 - 1);
		}break;
	}
}

/**
 * 当切换到该界面时触发
 */
static void onUI_intent(const Intent *intentPtr) {
    if (intentPtr != NULL) {
        //TODO
    }
}

/*
 * 当界面显示时触发
 */
static void onUI_show() {

}

/*
 * 当界面隐藏时触发
 */
static void onUI_hide() {

}

/*
 * 当界面完全退出时触发
 */
static void onUI_quit() {

}

/**
 * 串口数据回调接口
 */
static void onProtocolDataUpdate(const SProtocolData &data) {

}

/**
 * 定时器触发函数
 * 不建议在此函数中写耗时操作，否则将影响UI刷新
 * 参数： id
 *         当前所触发定时器的id，与注册时的id相同
 * 返回值: true
 *             继续运行当前定时器
 *         false
 *             停止运行当前定时器
 */
static bool onUI_Timer(int id){
	switch (id) {

		default:
			break;
	}
    return true;
}

/**
 * 有新的触摸事件时触发
 * 参数：ev
 *         新的触摸事件
 * 返回值：true
 *            表示该触摸事件在此被拦截，系统不再将此触摸事件传递到控件上
 *         false
 *            触摸事件将继续传递到控件上
 */
static bool onledActivityTouchEvent(const MotionEvent &ev) {
    switch (ev.mActionStatus) {
		case MotionEvent::E_ACTION_DOWN://触摸按下
			//LOGD("时刻 = %ld 坐标  x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
			break;
		case MotionEvent::E_ACTION_MOVE://触摸滑动
			break;
		case MotionEvent::E_ACTION_UP:  //触摸抬起
			break;
		default:
			break;
	}
	return false;
}
static void onProgressChanged_customBar(ZKSeekBar *pSeekBar, int progress) {
    //LOGD(" ProgressChanged customBar %d !!!\n", progress);
	float locktime = (progress + 1)* 30 / 60.0f;
    // 使用 ostringstream 格式化为字符串
    std::ostringstream oss;
    oss << std::fixed << std::setprecision(1) << locktime;
    mlightTimePtr->setText(oss.str() + "分钟");
}

static bool onButtonClick_OnSwitch(ZKButton *pButton) {
    LOGD(" ButtonClick OnSwitch !!!\n");
	mOnSwitchPtr->setSelected(false);
	mOffSwitchPtr->setSelected(false);
	mcustomSwitchPtr->setSelected(false);
	pButton->setSelected(true);
	mcustomWndPtr->hideWnd();
//	Global::Water::ledType = 0;
    return false;
}

static bool onButtonClick_customSwitch(ZKButton *pButton) {
    LOGD(" ButtonClick customSwitch !!!\n");
	mOnSwitchPtr->setSelected(false);
	mOffSwitchPtr->setSelected(false);
	mcustomSwitchPtr->setSelected(false);
	pButton->setSelected(true);
	mcustomWndPtr->showWnd();
	mcustomBarPtr->setProgress(Global::Water::ledTime / 30 - 1);
//	Global::Water::ledType = 2;
    return false;
}

static bool onButtonClick_OffSwitch(ZKButton *pButton) {
    LOGD(" ButtonClick OffSwitch !!!\n");
	mOnSwitchPtr->setSelected(false);
	mOffSwitchPtr->setSelected(false);
	mcustomSwitchPtr->setSelected(false);
	pButton->setSelected(true);
	mcustomWndPtr->hideWnd();
//	Global::Water::ledType = 1;
    return false;
}

static bool onButtonClick_ok(ZKButton *pButton) {
    LOGD(" ButtonClick ok !!!\n");


    if(mOnSwitchPtr->isSelected() ){
    	Global::Water::ledType = 1;
    	Global::Water::ledTime = 60;
    }else if(mOffSwitchPtr->isSelected() ){
    	Global::Water::ledType = 0;

    }else if(mcustomSwitchPtr->isSelected() ){
    	Global::Water::ledType = 2;
        Global::Water::ledTime = (mcustomBarPtr->getProgress() + 1 )*30;
    }
	Global::j_handler.saveInt("ledType", Global::Water::ledType);
	Global::j_handler.saveInt("ledTime", Global::Water::ledTime);
    EASYUICONTEXT->goBack();
    return false;
}

static bool onButtonClick_cancel(ZKButton *pButton) {
    LOGD(" ButtonClick cancel !!!\n");
    EASYUICONTEXT->goBack();
    return false;
}
static bool onButtonClick_sys_back(ZKButton *pButton) {
    LOGD(" ButtonClick sys_back !!!\n");
    return false;
}
